package com.manager.service.impl;

import com.manager.bean.*;
import com.manager.mapper.ValuableMapper;
import com.manager.service.ValuableService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Valuable)表服务实现类
 *
 * @author makejava
 * @since 2021-03-02 11:00:26
 */
@Service("valuableService")
public class ValuableServiceImpl implements ValuableService {
    @Resource
    private ValuableMapper valuableDao;

    //分页查询
    @Override
    public PageInfo<Valuable> findPageInfo(String name, String phone , Integer pageIndex, Integer pageSize) {
        PageInfo<Valuable> pi = new PageInfo<Valuable>();
        pi.setPageIndex(pageIndex);
        pi.setPageSize(pageSize);
        ValuableExample valuableExample=new ValuableExample();
        ValuableExample.Criteria criteria=valuableExample.createCriteria();
        if(name!=null&name!=""){
            criteria.andNameLike("%"+name+"%");
        }
        if(phone!=null&phone!=""){
            criteria.andPhoneEqualTo((phone));
        }
        //获取总条数
        Integer totalCount = valuableDao.countByExample(valuableExample);
        if (totalCount>0){
            pi.setTotalCount(totalCount);
            //每一页显示学生信息数
            //currentPage = (pageIndex-1)*pageSize  当前页码数减1*最大条数=开始行数
            int start=(pi.getPageIndex()-1)*pi.getPageSize();
            valuableExample.setOrderByClause("time+0 DESC");
            List<Valuable> valuableList =	valuableDao.selectByExample(valuableExample);
            int end=start+pi.getPageSize();
            if(end>valuableList.size()-1){
                end=valuableList.size();
            }
            valuableList=valuableList.subList(start,end);
            pi.setList(valuableList);
        }
        return pi;
    }
    //无
    @Override
    public Valuable queryById(Integer Id) {
        return this.valuableDao.selectByPrimaryKey(Id);
    }


    @Override
    public List<Valuable> getAll(){
        ValuableExample valuableExample = new ValuableExample();
        ValuableExample.Criteria criteria = valuableExample.createCriteria();
        return valuableDao.selectByExample(valuableExample);
    }

    /**
     * 新增数据
     *
     * @param valuable 实例对象
     * @return 实例对象
     */
    @Override
    public Valuable insert(Valuable valuable) {
        this.valuableDao.insert(valuable);
        return valuable;
    }

    /**
     * 修改数据
     *
     * @param valuable 实例对象
     * @return 实例对象
     */
    @Override
    public Valuable update(Valuable valuable) {
        valuableDao.updateByPrimaryKey(valuable);
        return new Valuable();
        //
    }

    @Override
    public boolean deleteById(Integer Id) {
        this.valuableDao.deleteByPrimaryKey(Id);
        return true;
    }

}
